package com.atguigu.flink.chapter10;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/*
普通的kafka无法写入具有更新表的数据
 */
public class UpsertKafkaConnectorDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        env.enableCheckpointing(3000);
        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        //通过ddl的方式创建连接器
        tEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int" +
                ") with(" +
                "  'connector' = 'kafka', " +
                "  'topic' = 's1', " +
                "  'properties.bootstrap.servers' = 'hadoop102:9092', " +
                "  'properties.group.id' = 'atguigu', " +
                "  'scan.startup.mode' = 'latest-offset', " +
                "  'format' = 'csv' " +
                ")");

        Table result = tEnv.sqlQuery("select id,sum(vc) as vc_sum from sensor group by id");

        tEnv.executeSql("create table s2(" +
                "id string," +
                "vc_sum int," +
                "primary key(id) not enforced" +   //flink不支持强迫模式
                ") with(" +
                "  'connector' = 'upsert-kafka'," +
                "  'topic' = 's2_upsert', " +
                "  'properties.bootstrap.servers' = 'hadoop102:9092', " +
                "  'key.format' = 'json', " +
                "  'value.format' = 'json' " +
                ")");
        result.executeInsert("s2");
    }
}


//public class UpsertKafkaConnectorDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//        env.enableCheckpointing(3000);
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
//
//        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
//        tEnv.executeSql("create table sensor(" +
//                "id string," +
//                "ts bigint," +
//                "vc int" +
//                ") with(" +
//                " 'connector' = 'kafka', " +
//                " 'topic' = 's1', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'properties.group.id' = 'atguigu', " +
//                " 'scan.startup.mode' = 'latest-offset', " +
//                " 'format' = 'csv' " +
//                ")");
//
//        Table result = tEnv.sqlQuery("select id,sum(vc) as vc_sum from sensor group by id");
//
//        tEnv.executeSql("create table s2(" +
//                "id string," +
//                "vc_sum int," +
//                "primary key(id) not enforced" +
//                ") with(" +
//                " 'connector' = 'upsert-kafka', " +
//                " 'topic' = 's2_upsert', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'key.format' = 'json', " +
//                " 'value.format' = 'json' " +
//                ")");
//
//        result.executeInsert("s2");
//    }
//}

//public class UpsertKafkaConnectorDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//        env.enableCheckpointing(3000);
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
//
//        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
//        //通过ddl的方式创建连接器
//        tEnv.executeSql("create table sensor(" +
//                "id string," +
//                "ts bigint," +
//                "vc int" +
//                ") with(" +
//                " 'connector' = 'kafka', " +
//                " 'topic' = 's1', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'properties.group.id' = 'atguigu', " +
//                " 'scan.startup.mode' = 'latest-offset', " +
//                " 'format' = 'csv' " +
//                ")");
//
//
//        Table result = tEnv.sqlQuery("select id,sum(vc) as vc_sum from sensor group by id");
//
//        tEnv.executeSql("create table s2(" +
//                "id string," +
//                "vc_sum int," +
//                "primary key(id) not enforced" +
//                ") with(" +
//                " 'connector' = 'upsert-kafka', " +
//                " 'topic' = 's2_upsert', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'key.format' = 'json', " +
//                " 'value.format' = 'json' " +
//                ")");
//
//        result.executeInsert("s2");
//    }
//}

//public class UpsertKafkaConnectorDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        env.setParallelism(1);
//        env.enableCheckpointing(3000);
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
//
//
//        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
//
//        tEnv.executeSql("create table sensor(" +
//                "id string," +
//                "ts bigint," +
//                "vc int" +
//                ") with(" +
//                " 'connector' = 'kafka', " +
//                " 'topic' = 's1', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'properties.group.id' = 'atguigu', " +
//                " 'scan.startup.mode' = 'latest-offset', " +
//                " 'format' = 'csv' " +
//                ")");
//
//        Table result = tEnv.sqlQuery("select id,sum(vc) as vc_sum from sensor group by id");
//
//        tEnv.executeSql("create table s2(" +
//                "id string," +
//                "vc_sum int," +
//                "primary key(id) not enforced" +
//                ") with(" +
//                " 'connector' = 'upsert-kafka', " +
//                " 'topic' = 's2_upsert', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'key.format' = 'json', " +
//                " 'value.format' = 'json' " +
//                ")");
//
//        result.executeInsert("s2");
//    }
//}